﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; 

namespace NETCORE.CAP
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);


            services.AddDbContext<CapDbContext>(options =>
            options.UseMySql(Configuration.GetConnectionString("Mysql_Conn")));


            services.AddCap(x =>
            {
                //如果你使用的 EF 进行数据操作，你需要添加如下配置：
                x.UseEntityFramework<CapDbContext>();  //可选项，你不需要再次配置 x.UseSqlServer 了

                //如果你使用的ADO.NET，根据数据库选择进行配置：
                //x.UseSqlServer("数据库连接字符串");
                //x.UseMySql("server=localhost;port=3306;userid=root;password=123456;database=db1;SslMode=none");
                //x.UsePostgreSql("数据库连接字符串");

                //如果你使用的 MongoDB，你可以添加如下配置：
                //x.UseMongoDB("ConnectionStrings");  //注意，仅支持MongoDB 4.0+集群

                //CAP支持 RabbitMQ、Kafka、AzureServiceBus 等作为MQ，根据使用选择配置：
                x.UseRabbitMQ(o =>
                {
                    o.HostName = Configuration.GetSection("RabbitMQ")["HostName"];
                    o.UserName = Configuration.GetSection("RabbitMQ")["UserName"];
                    o.Password = Configuration.GetSection("RabbitMQ")["Password"];
                    o.VirtualHost = Configuration.GetSection("RabbitMQ")["VirtualHost"];
                    o.Port = Convert.ToInt32(Configuration.GetSection("RabbitMQ")["Port"]);
                    //指定Topic exchange名称，不指定的话会用默认的
                    o.ExchangeName = Configuration.GetSection("RabbitMQ")["ExchangeName"];

                });

                //设置处理成功的数据在数据库中保存的时间（秒），为保证系统新能，数据会定期清理。
                x.SucceedMessageExpiredAfter = 24 * 3600;

                //设置失败重试次数
                x.FailedRetryCount = 5;

                //x.UseKafka("ConnectionStrings");
                //x.UseAzureServiceBus("ConnectionStrings");

                x.UseDashboard();
            });

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
             
            app.UseMvc();
        }
    }
}
